//
//  ZLMainViewController.swift
//  MVVM微博
//
//  Created by AppleDev on 2017/7/15.
//  Copyright © 2017年 AppleDev. All rights reserved.
//

import UIKit

class ZLMainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildControllers()
        
        setupComposeButton()
        
    }
    
    public lazy var composeButton:UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    func composeStatus(){
        print("撰写微博")
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        
        return .portrait
        
    }
    
    
}

extension ZLMainViewController{
    
     func setupComposeButton(){
        
        let count = CGFloat(childViewControllers.count)
        
        let w = tabBar.bounds.width / count - 1
        
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        
        composeButton.addTarget(self, action: #selector(composeStatus), for: .touchUpInside)
        
        tabBar.addSubview(composeButton)
        
    }
    /// 设置子控制器
    public func setupChildControllers(){
        
        // 先从沙盒中取出
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        
        let jsonPath = (path as NSString).appendingPathComponent("main.json")
        
        var  data = NSData(contentsOfFile: jsonPath)
        
        if data == nil {
            
            let file = Bundle.main.path(forResource: "main.json", ofType: nil)
            
             data = NSData(contentsOfFile: file!)
            
        }

       guard
        let array = try? JSONSerialization.jsonObject(with: data! as Data, options: []) as? [[String:Any]]
        else{
            return
        }

        
        var arrayM = [UIViewController]()
        
        for dict in array! {
            
            arrayM.append(controller(dict: dict))
            
        }
        
        viewControllers = arrayM
        
    }
    
    /// 使用字典创建一个子控制器
    ///
    /// - Parameter dict: 字典 clsName title imageName
    /// - Returns: 子控制器
    private func controller(dict:[String:Any]) ->(UIViewController){
        guard let clsName = dict["clsName"] as? String,
            let title = dict["title"]as? String,
            let imageName = dict["imageName"]as? String,

            let visitorDic = dict["visitorInfo"] as?[String:String],
            let  cls = NSClassFromString(Bundle.main.namespace + "."+"" + clsName) as? ZLBaseViewController.Type
            else {
                return UIViewController()
                
        }

        let vc = cls.init()
        vc.title = title
        vc.visitorInfo = visitorDic
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName+"_selected")?.withRenderingMode(.alwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 13)], for: .normal)
        let nav = ZLMainNavigationController(rootViewController: vc)
        return nav
        
    }
    
}
